<#include "admin/common/include/sysparam.html">


<div class="row-fluid">
	<div class="span12">
		<!-- BEGIN EXTRAS PORTLET-->
		<div class="portlet box">
			<div class="portlet-title">
				<div class="caption">
					<h5>运费模板编辑</h5>
				</div>
			</div>
			<div class="portlet-body form">
				<div class="alert alert-error " id="errtip" style="display: none;"></div>

				<!-- BEGIN FORM-->
				<form   onSubmit="return false;"
					id="carryAddForm" class="form-horizontal" method="post">
					<input type="hidden" name="id" id="id" value="${(bean.id)?if_exists}" />
					<div class="control-group">
						<label class="control-label">模板名称<span class="required">*</span></label>
						<div class="controls">
							<input type="text" name="name" id="name" require="true"
								datatype="require" msg="组名称不能为空" class="span3 m-wrap"
								value="${(bean.name)?if_exists}" />
						</div>
					</div>


					<div class="control-group">
						<label class="control-label">配送方式<span class="required">*</span></label>
						<div class="controls"><@checkbox paramCode=carryWay
							valueList=(checkCarryWayList)?if_exists item='name="carryWays"'/></div>

					</div>
					<div class="control-group">
						<label class="control-label">运费设置<span class="required">*</span></label>
						<div class="controls">
							<#list carryWay as code>
							<table class=" table table-bordered table-hover"
								name="carryWayTable_${code.paramValue}" style="display: none">
								<thead>
									<tr>
										<th>${code.paramName}运费设置</th>
									</tr>
								</thead>
								<tbody class="param-tbody">
									<tr>
										<td width=30%>配送区域</td>
										<td>首N件</td>
										<td>首费(￥)</td>
										<td>续M件</td>
										<td>继费(￥)</td>
									</tr>
									<#assign hasCarryWay=0 />
									<#list carryWayDataList as carryWayData>
										 <#if code.paramValue = carryWayData.carryWay>
										 <#assign hasCarryWay=1 />
										<tr>
										    <#if carryWayData.regionType = 0>
												<td>全国默认地区<input type="hidden" name="regionType" value="0"/></td>
											<#else>
												<td>指定区域: <span name="regionName"  >${(carryWayData.regionName)?if_exists}</span>
												<br><a href="#" name="regionEdit" onclick="">编辑</a> <a href='#'
													name="edit_action" onclick="$(this).parents('tr')[0].remove();">删除</a>
											  			<input type="hidden" name="regionType" value="1"/></td>
											</#if>
											<td><input class="m-wrap long span12" name="firstAmount"
												type="text" value="${(carryWayData.firstAmount)?if_exists}"/></td>
											<td><input class="m-wrap long span12" name="firstFee"
												type="text"  value="${(carryWayData.firstFee)?if_exists}" /></td>
											<td><input class="m-wrap long span12" name="secAmount"
												type="text"  value="${(carryWayData.secAmount)?if_exists}" /></td>
											<td><input class="m-wrap long span12" name="secFee"
												type="text"  value="${(carryWayData.secFee)?if_exists}" /></td>
										</tr>
									</#if>
									</#list>
									<!-- 如果没有设置运费方式则显示默认可填的 -->
									<#if hasCarryWay =0 >
									<tr>
											<td>全国默认地区<input type="hidden" name="regionType" value="0"/></td>
											<td><input class="m-wrap long span12" name="firstAmount"
												type="text" value="${(carryWayData.firstAmount)?if_exists}"/></td>
											<td><input class="m-wrap long span12" name="firstFee"
												type="text"  value="${(carryWayData.firstFee)?if_exists}" /></td>
											<td><input class="m-wrap long span12" name="secAmount"
												type="text"  value="${(carryWayData.secAmount)?if_exists}" /></td>
											<td><input class="m-wrap long span12" name="secFee"
												type="text"  value="${(carryWayData.secFee)?if_exists}" /></td>
										</tr>
									</#if>
									<tr>
										<td colspan="5"><a href="#" class="btn add-region">为指定地区设置运费</a>
										</td>
									</tr>
								</tbody>
							</table>
							</#list>
						</div>

					</div>

					<div class="form-actions">
						<button type="button" id="carry_save_btn" class="btn blue">保存</button>
						<button type="button" id="carry_return_btn" class="btn  ">关闭</button>
					</div>
				</form>
			</div>
			<!-- END EXTRAS PORTLET-->
		</div>
	</div>
</div>

<div id="carryRegionHtmlTmp" style="display: none;"></div>
<script>
   
	jQuery(document).ready(function() {
		App.init();
		bindAddRegion();
		bindAddCarryWay();
		bindSaveCarryWay();
		bindAreaEdit(); 
		generateIdForRegionName();
		$("#carry_return_btn").bind("click", function() {
			var url = '${contextPath}/Admin-Carry-show.action';
			mainFrame.addContainer(url);
		});
	});
	
	function bindAreaEdit(){
		$("a[name='regionEdit']").bind("click", function() {   
			var selectRegionId = $(this).parent().find('span').attr('id'); 
			city.showCity(selectRegionId);
		});
		
	}
	/**为regionName生成ID*/
	function generateIdForRegionName(){
		var newRegionNameIndex = 0;
		//为区域名称span生成id
		$("span[name='regionName']").each(function(){ 
			 newRegionNameIndex += 1;
			 $(this).attr('id',"regionName_"+newRegionNameIndex); 
		});
	}
	
	function bindAddRegion() {
		$(".add-region").bind("click", function() {
			var tbody = $(this).parents(".param-tbody");
			var parentTr = $(this).parent().parent();
			$("#carryRegionHtmlTmp").html('');
			$("#carryRegionHtmlTmp").loadTemplate($("#carryRegionTemplate"));
			var trhtml = $("#carryRegionHtmlTmp").html();
			$(trhtml).insertBefore(parentTr);
			bindAreaEdit();
			generateIdForRegionName();
		}); 
	}

	function bindAddCarryWay() {
		$(":checkbox[name='carryWays']").bind("click", function() {
			//$('#carryWayTable_0').hide(); 
			var checked = $(this).is(':checked'); 
			var value = $(this).val(); 
			var tableName= 'carryWayTable_'+value;
			var carryTableObj = $("table[name='"+tableName+"']"); 
			if(checked)
				carryTableObj.show();
			else
				carryTableObj.hide();
		});
		
		$(":checkbox[name='carryWays']").each(function() { 
			var checked = $(this).is(':checked');  
			var value = $(this).val(); 
			var tableName= 'carryWayTable_'+value;
			var carryTableObj = $("table[name='"+tableName+"']"); 
			if(checked)
				carryTableObj.show();
		});
	}
	
	function bindSaveCarryWay(){
		$("#carry_save_btn").bind("click", function() { 
			var webCarryTemplate={};
			if($('#id').val()){
				webCarryTemplate["id"]= $('#id').val();
			}
			webCarryTemplate["name"]=$('#name').val();
			webCarryTemplate["carryWays"]=common.getCheckBoxValue('carryWays',',');
			var webCarryWayList = new Array();
			 $("table[name^='carryWayTable_']").each(function(){ 
				 var tableName = $(this).attr('name');
				 var carryWay = tableName.split('_')[1];
				 if(!common.isCheckBoxValue('carryWays',carryWay))
					 return true; 
			 
				 $(this).find("input[name='regionType']").each(function(){
					 var parentTr = $(this).parent().parent();
					 var dataArray={}; 
					 var regionType =  $(parentTr).find("input[name='regionType']");
					 var regionName =  $(parentTr).find("span[name='regionName']");
					 var firstAmount =  $(parentTr).find("input[name='firstAmount']");
					 var firstFee =  $(parentTr).find("input[name='firstFee']");
					 var secAmount =  $(parentTr).find("input[name='secAmount']");
					 var secFee =  $(parentTr).find("input[name='secFee']");
					 dataArray["carryWay"] = carryWay;
					 dataArray["regionType"] = $(regionType).val();
					 dataArray["firstAmount"] = parseInt($(firstAmount).val());
					 dataArray["firstFee"] = parseFloat($(firstFee).val());
					 dataArray["secAmount"] = parseInt($(secAmount).val());
					 dataArray["secFee"] =parseFloat($(secFee).val());
					 dataArray["regionName"] =$(regionName).html(); 
					 webCarryWayList.push(dataArray);
				 });
			 });  
			
			 if(AI.Validator.validForm('carryAddForm', 1, 'errtip')
					 && checkSaveCarryWay(webCarryWayList)){ 
				 var webCarryWayListJson = JSON.stringify(webCarryWayList); 
				 var webCarryTemplateJson= JSON.stringify(webCarryTemplate); 
				 var param = "webCarryWayList="+webCarryWayListJson;
				     param += "&webCarryTemplate="+webCarryTemplateJson;  
			 	 ajax.syncJsonRequest({
						url : '${contextPath}/Admin-Carry-save.action',
						param : param,
						success : function(data) {
							if (!data || !data.success) { 
								common.alert({
									content : data.retinfo,
									closeFn : function() {  
									}
								});
							}
							if (data.success) { 
								$('#id').val(data.result);
								common.alert({
									content : '保存成功！',
									closeFn : function() {  
									}
								});
							}
						}
					});   
			 }
		}); 
	}
	
	function checkSaveCarryWay(dataList){  
		 for(var i=0;i<dataList.length;i++){ 
		     if(dataList[i].regionType==1 && !dataList[i].regionName){
		    	 common.alert({
						content : '请填写完整模板的指定区域！',
						closeFn : function() {  
						}
					});
				return false;
		     }
			 if(!dataList[i].firstFee || !dataList[i].firstAmount){  
				 common.alert({
						content : '请正确填写完整的运费费用！',
						closeFn : function() {  
						}
					});
				return false;
			 }
			 if((dataList[i].secAmount && !dataList[i].secFee)
					 || (!dataList[i].secAmount && dataList[i].secFee)){  
				 common.alert({
						content : '请正确填写完整的运费费用！',
						closeFn : function() {  
						}
					});
				return false;
			 }
		 } 
		 return true;
	}
 

</script>
<!--运费模板区域-->
<script type="text/html" id="carryRegionTemplate">
<tr class="odd gradeX">
	<td>指定区域: <span name="regionName"></span><br><a href="#" name="regionEdit" onclick="">编辑</a> <a href='#'
		name="edit_action" onclick="$(this).parents('tr')[0].remove();">删除</a>
  			<input type="hidden" name="regionType" value="1"/>
	</td>
	<td><input class="m-wrap long span12" name="firstAmount"
		type="text" /></td>
	<td><input class="m-wrap long span12" name="firstFee" type="text" />
	</td>
	<td><input class="m-wrap long span12" name="secAmount" type="text" />
	</td>
	<td><input class="m-wrap long span12" name="secFee" type="text" />
	</td>
</tr>
	</script>